{"ast":null,"code":"import _regeneratorRuntime from\"/Users/poppie/Desktop/development/messenger/app/frontend/node_modules/@babel/runtime/helpers/esm/regeneratorRuntime.js\";import _asyncToGenerator from\"/Users/poppie/Desktop/development/messenger/app/frontend/node_modules/@babel/runtime/helpers/esm/asyncToGenerator.js\";import _classCallCheck from\"/Users/poppie/Desktop/development/messenger/app/frontend/node_modules/@babel/runtime/helpers/esm/classCallCheck.js\";import _createClass from\"/Users/poppie/Desktop/development/messenger/app/frontend/node_modules/@babel/runtime/helpers/esm/createClass.js\";import{makeAutoObservable}from\"mobx\";import{UserService,AuthService,ChatService}from\"../services/\";import axios from\"axios\";import{refreshToken}from\"../routes\";var Store=/*#__PURE__*/function(){function Store(){var _this=this;_classCallCheck(this,Store);this.isAuth=false;this.profile={};this.chats={};this.isLoading=false;this.isError=false;this.getAuth=function(){return _this.isAuth;};this.getLoading=function(){return _this.isLoading;};makeAutoObservable(this);}_createClass(Store,[{key:\"setAuth\",value:function setAuth(bool){this.isAuth=bool;}},{key:\"getProfile\",value:function getProfile(){return this.profile;}},{key:\"setProfile\",value:function setProfile(profile){this.profile=profile;}},{key:\"getChats\",value:function getChats(){return this.chats;}},{key:\"setChats\",value:function setChats(chats){this.chats=chats;}},{key:\"setLoading\",value:function setLoading(bool){this.isLoading=bool;}},{key:\"setIsError\",value:function setIsError(bool){this.isError=bool;}},{key:\"getIsError\",value:function getIsError(){return this.isError;}},{key:\"setTokensTolLocalStorage\",value:function setTokensTolLocalStorage(response){localStorage.setItem(\"\".concat(process.env.REACT_APP_NAME_TOKEN),response.data.token);localStorage.setItem(\"\".concat(process.env.REACT_APP_NAME_REFRESH_TOKEN),response.data.refresh_token);}},{key:\"removeTokensTolLocalStorage\",value:function removeTokensTolLocalStorage(){localStorage.removeItem(\"\".concat(process.env.REACT_APP_NAME_TOKEN));localStorage.removeItem(\"\".concat(process.env.REACT_APP_NAME_REFRESH_TOKEN));}},{key:\"getProfileFromAPI\",value:function(){var _getProfileFromAPI=_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee(){var response;return _regeneratorRuntime().wrap(function _callee$(_context){while(1){switch(_context.prev=_context.next){case 0:_context.prev=0;_context.next=3;return UserService.profile();case 3:response=_context.sent;this.setProfile(response.data);_context.next=11;break;case 7:_context.prev=7;_context.t0=_context[\"catch\"](0);this.removeTokensTolLocalStorage();this.setIsError(true);case 11:case\"end\":return _context.stop();}}},_callee,this,[[0,7]]);}));function getProfileFromAPI(){return _getProfileFromAPI.apply(this,arguments);}return getProfileFromAPI;}()},{key:\"signIn\",value:function(){var _signIn=_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee2(request){var response;return _regeneratorRuntime().wrap(function _callee2$(_context2){while(1){switch(_context2.prev=_context2.next){case 0:_context2.prev=0;_context2.next=3;return AuthService.signIn(request);case 3:response=_context2.sent;this.setTokensTolLocalStorage(response);this.setAuth(true);_context2.next=8;return this.getProfileFromAPI();case 8:_context2.next=13;break;case 10:_context2.prev=10;_context2.t0=_context2[\"catch\"](0);this.setIsError(true);case 13:case\"end\":return _context2.stop();}}},_callee2,this,[[0,10]]);}));function signIn(_x){return _signIn.apply(this,arguments);}return signIn;}()},{key:\"signUp\",value:function(){var _signUp=_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee3(request){var response;return _regeneratorRuntime().wrap(function _callee3$(_context3){while(1){switch(_context3.prev=_context3.next){case 0:_context3.prev=0;_context3.next=3;return AuthService.signUp(request);case 3:response=_context3.sent;this.setTokensTolLocalStorage(response);this.setAuth(true);_context3.next=8;return this.getProfileFromAPI();case 8:_context3.next=13;break;case 10:_context3.prev=10;_context3.t0=_context3[\"catch\"](0);this.setIsError(true);case 13:case\"end\":return _context3.stop();}}},_callee3,this,[[0,10]]);}));function signUp(_x2){return _signUp.apply(this,arguments);}return signUp;}()},{key:\"forgotPassword\",value:function(){var _forgotPassword=_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee4(request){return _regeneratorRuntime().wrap(function _callee4$(_context4){while(1){switch(_context4.prev=_context4.next){case 0:_context4.prev=0;_context4.next=3;return AuthService.forgotPassword(request);case 3:_context4.next=8;break;case 5:_context4.prev=5;_context4.t0=_context4[\"catch\"](0);this.setIsError(true);case 8:case\"end\":return _context4.stop();}}},_callee4,this,[[0,5]]);}));function forgotPassword(_x3){return _forgotPassword.apply(this,arguments);}return forgotPassword;}()},{key:\"logout\",value:function logout(){this.removeTokensTolLocalStorage();this.setAuth(false);this.setProfile({});}},{key:\"checkAuth\",value:function(){var _checkAuth=_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee5(){var response;return _regeneratorRuntime().wrap(function _callee5$(_context5){while(1){switch(_context5.prev=_context5.next){case 0:this.setLoading(true);_context5.prev=1;_context5.next=4;return axios.post(refreshToken(),{\"refresh_token\":localStorage.getItem(\"\".concat(process.env.REACT_APP_NAME_REFRESH_TOKEN))});case 4:response=_context5.sent;this.setTokensTolLocalStorage(response);this.setAuth(true);_context5.next=9;return this.getProfileFromAPI();case 9:_context5.next=15;break;case 11:_context5.prev=11;_context5.t0=_context5[\"catch\"](1);this.removeTokensTolLocalStorage();this.setIsError(true);case 15:_context5.prev=15;this.setLoading(false);return _context5.finish(15);case 18:case\"end\":return _context5.stop();}}},_callee5,this,[[1,11,15,18]]);}));function checkAuth(){return _checkAuth.apply(this,arguments);}return checkAuth;}()},{key:\"getChatsFromAPI\",value:function(){var _getChatsFromAPI=_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee6(){var _this2=this;return _regeneratorRuntime().wrap(function _callee6$(_context6){while(1){switch(_context6.prev=_context6.next){case 0:_context6.prev=0;_context6.next=3;return ChatService.getChatsList().then(function(res){return _this2.setChats(res.data);});case 3:return _context6.abrupt(\"return\",_context6.sent);case 6:_context6.prev=6;_context6.t0=_context6[\"catch\"](0);console.error(_context6.t0.response.data.message);case 9:case\"end\":return _context6.stop();}}},_callee6,null,[[0,6]]);}));function getChatsFromAPI(){return _getChatsFromAPI.apply(this,arguments);}return getChatsFromAPI;}()}]);return Store;}();export{Store as default};","map":{"version":3,"names":["makeAutoObservable","UserService","AuthService","ChatService","axios","refreshToken","Store","isAuth","profile","chats","isLoading","isError","getAuth","getLoading","bool","response","localStorage","setItem","process","env","REACT_APP_NAME_TOKEN","data","token","REACT_APP_NAME_REFRESH_TOKEN","refresh_token","removeItem","setProfile","removeTokensTolLocalStorage","setIsError","request","signIn","setTokensTolLocalStorage","setAuth","getProfileFromAPI","signUp","forgotPassword","setLoading","post","getItem","getChatsList","then","res","setChats","console","error","message"],"sources":["/Users/poppie/Desktop/development/messenger/app/frontend/src/store/store.ts"],"sourcesContent":["import {makeAutoObservable} from \"mobx\";\nimport SignInRequest from \"../models/request/SignInRequest\";\nimport SignUpRequest from \"../models/request/SignUpRequest\";\nimport {UserService, AuthService, ChatService} from \"../services/\";\nimport {UserListItem} from \"../models/response/UserListItem\";\nimport axios, {AxiosResponse} from \"axios\";\nimport {AuthResponse} from \"../models/response/AuthResponse\";\nimport {refreshToken} from \"../routes\";\nimport ForgotPasswordRequest from \"../models/request/ForgotPasswordRequest\";\nimport {ChatsListResponse} from \"../models/response/ChatsListResponse\";\n\nexport default class Store {\n\n    private isAuth: boolean = false;\n    private profile = {} as UserListItem;\n    public chats = {} as ChatsListResponse;\n    private isLoading: boolean = false;\n    private isError: boolean = false;\n\n    public constructor() {\n        makeAutoObservable(this);\n    }\n\n    private setAuth(bool: boolean): void {\n        this.isAuth = bool;\n    }\n\n    public getAuth: () => boolean = () => {\n        return this.isAuth;\n    }\n\n    public getProfile(): UserListItem {\n        return this.profile;\n    }\n\n    private setProfile(profile: UserListItem): void {\n        this.profile = profile;\n    }\n\n    public getChats(): ChatsListResponse {\n        return this.chats;\n    }\n\n    private setChats(chats: ChatsListResponse): void {\n        this.chats = chats;\n    }\n\n    private setLoading(bool: boolean): void {\n        this.isLoading = bool;\n    }\n\n    public getLoading: () => boolean = () => {\n        return this.isLoading;\n    }\n\n    public setIsError(bool: boolean): void {\n        this.isError = bool;\n    }\n\n    public getIsError(): boolean {\n        return this.isError;\n    }\n\n    public setTokensTolLocalStorage(response: AxiosResponse<AuthResponse>): void {\n        localStorage.setItem(\n            `${process.env.REACT_APP_NAME_TOKEN}`,\n            response.data.token\n        );\n        localStorage.setItem(\n            `${process.env.REACT_APP_NAME_REFRESH_TOKEN}`,\n            response.data.refresh_token\n        );\n    }\n\n    public removeTokensTolLocalStorage(): void {\n        localStorage.removeItem(`${process.env.REACT_APP_NAME_TOKEN}`);\n        localStorage.removeItem(`${process.env.REACT_APP_NAME_REFRESH_TOKEN}`);\n    }\n\n    private async getProfileFromAPI() {\n        try {\n            const response = await UserService.profile();\n            this.setProfile(response.data);\n        } catch (e) {\n            this.removeTokensTolLocalStorage();\n            this.setIsError(true);\n        }\n    }\n\n    public async signIn(request: SignInRequest) {\n        try {\n            const response = await AuthService.signIn(request);\n            this.setTokensTolLocalStorage(response);\n            this.setAuth(true);\n            await this.getProfileFromAPI();\n        } catch (e) {\n            this.setIsError(true);\n        }\n    }\n\n    public async signUp(request: SignUpRequest) {\n        try {\n            const response = await AuthService.signUp(request);\n            this.setTokensTolLocalStorage(response);\n            this.setAuth(true);\n            await this.getProfileFromAPI();\n        } catch (e) {\n            this.setIsError(true);\n        }\n    }\n\n    public async forgotPassword(request: ForgotPasswordRequest) {\n        try {\n            await AuthService.forgotPassword(request);\n        } catch (e) {\n            this.setIsError(true);\n        }\n    }\n\n    public logout() {\n        this.removeTokensTolLocalStorage();\n        this.setAuth(false);\n        this.setProfile({} as UserListItem);\n    }\n\n    public async checkAuth() {\n        this.setLoading(true);\n        try {\n            const response = await axios.post<AuthResponse>(\n                refreshToken(),\n                {\n                    \"refresh_token\": localStorage.getItem(`${process.env.REACT_APP_NAME_REFRESH_TOKEN}`)\n                }\n            );\n            this.setTokensTolLocalStorage(response);\n            this.setAuth(true);\n            await this.getProfileFromAPI();\n        } catch (e) {\n            this.removeTokensTolLocalStorage();\n            this.setIsError(true);\n        } finally {\n            this.setLoading(false);\n        }\n    }\n\n    public async getChatsFromAPI() {\n        try {\n            return await ChatService.getChatsList().then((res: any) => (\n                this.setChats(res.data)\n            ));\n        } catch (e: any) {\n            console.error(e.response.data.message);\n        }\n    }\n}"],"mappings":"skBAAA,OAAQA,kBAAkB,KAAO,MAAM,CAGvC,OAAQC,WAAW,CAAEC,WAAW,CAAEC,WAAW,KAAO,cAAc,CAElE,MAAOC,MAAK,KAAuB,OAAO,CAE1C,OAAQC,YAAY,KAAO,WAAW,CAAC,GAIlBC,MAAK,yBAQtB,gBAAqB,iDANbC,MAAM,CAAY,KAAK,MACvBC,OAAO,CAAG,CAAC,CAAC,MACbC,KAAK,CAAG,CAAC,CAAC,MACTC,SAAS,CAAY,KAAK,MAC1BC,OAAO,CAAY,KAAK,MAUzBC,OAAO,CAAkB,UAAM,CAClC,MAAO,MAAI,CAACL,MAAM,CACtB,CAAC,MAsBMM,UAAU,CAAkB,UAAM,CACrC,MAAO,MAAI,CAACH,SAAS,CACzB,CAAC,CAjCGV,kBAAkB,CAAC,IAAI,CAAC,CAC5B,CAAC,yCAED,iBAAgBc,IAAa,CAAQ,CACjC,IAAI,CAACP,MAAM,CAAGO,IAAI,CACtB,CAAC,0BAMD,qBAAkC,CAC9B,MAAO,KAAI,CAACN,OAAO,CACvB,CAAC,0BAED,oBAAmBA,OAAqB,CAAQ,CAC5C,IAAI,CAACA,OAAO,CAAGA,OAAO,CAC1B,CAAC,wBAED,mBAAqC,CACjC,MAAO,KAAI,CAACC,KAAK,CACrB,CAAC,wBAED,kBAAiBA,KAAwB,CAAQ,CAC7C,IAAI,CAACA,KAAK,CAAGA,KAAK,CACtB,CAAC,0BAED,oBAAmBK,IAAa,CAAQ,CACpC,IAAI,CAACJ,SAAS,CAAGI,IAAI,CACzB,CAAC,0BAMD,oBAAkBA,IAAa,CAAQ,CACnC,IAAI,CAACH,OAAO,CAAGG,IAAI,CACvB,CAAC,0BAED,qBAA6B,CACzB,MAA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